<?php
namespace Hdphp\Libs;
class App{
    public static function run(){
        //初始化框架
        self::init();
        //当实例化一个未找见的类 自动触发当前类的auto方法
        spl_autoload_register([__CLASS__,"auto"]);
        App::autoRun();
    }

    /**
     *
     */
    private static function auto($cname){
        if (substr($cname,-10 )== "Controller"){
            $path = "App/" . str_replace("\\","/" ,$cname ) . ".php";
            //p($path);
        }else{
            $path = str_replace("\\","/" ,$cname ). ".php";

        }
        require $path;
    }

    /**
     * 自动载入的功能
     */
    private static function autoRun(){
        $m = MODULE;
        $c = CONTROLLER;
        $a = ACTION;
        //首先写死【\Home\Controller\IndexController】，进行该地址的实例化，在这些的时候命名空间是没有问题的，但是会报错，找不见类【原因是类没有被载入】
        $controller = "\\{$m}\Controller\\{$c}Controller";
        $obj = new $controller;
        $obj->$a();
    }

    /**
     * 初始化方法
     */
    private static function init(){
        $m = isset($_GET["m"]) ? $_GET["m"] : "Home";
        define("MODULE",$m);
        $c = isset($_GET["c"]) ? $_GET["c"] : "view";
        define("CONTROLLER",$c);
        $a = isset($_GET["a"]) ? $_GET["a"] : "Index";
        define("ACTION",$a);
        //先加载系统配置项
        $systemConfig = include "Config.php";
        //p($systemConfig);
        C($systemConfig);
        //加载用户配置项
        $userConfig = include "App/" .MODULE . "/Config/Config.php";
        //var_dump($userConfig);???
        C($userConfig);
        //设置时区
        date_default_timezone_set(C("TIMEZONE"));
        //开启session
        session_id() || session_start();
    }
}